import { idByGetDto, idByPostDto, pageOrSortDto } from '@/lib/dto/baseDto';
import { ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import {
  IsMobilePhone,
  IsNotEmpty,
  IsString,
  Matches,
  MaxLength,
  MinLength,
  ValidateNested,
} from 'class-validator';

export class ManagerInsetDto {
  @ApiProperty({ example: '小明' })
  @IsString({ message: '管理名称需要是字符串' })
  @MaxLength(20, { message: '管理名称不能超过20个字符' })
  @IsNotEmpty({ message: '管理名称不能为空' })
  manager_name: string;

  @Matches(/^[a-zA-Z]\w{5,17}$/, {
    message:
      '密码不正确:以字母开头，长度在6~18之间，只能包含字母、数字和下划线',
  })
  @MaxLength(18, { message: '密码不能超过18个字符' })
  @MinLength(6, { message: '密码不能少于6个字符' })
  @IsNotEmpty({ message: '密码不能为空' })
  @ApiProperty({ example: 'wangjing123' })
  password: string;

  @Matches(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/, {
    message: '邮箱格式不正确',
  })
  @ApiProperty({ example: '447405759@qq.com' })
  eamil: string;

  @Matches(
    /^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/,
    { message: '手机号不正确' },
  )
  @IsMobilePhone('zh-CN')
  @MaxLength(11, { message: '手机号不能超过11个字符' })
  @IsNotEmpty({ message: '手机号不能为空' })
  @ApiProperty({ example: '18381532620' })
  phone: string;

  @ApiProperty()
  wx_openid: string;
}

export class ManagerDelteDto extends idByPostDto {}

export class ManagerFindDto extends idByGetDto {}
export class ManagerListDto extends pageOrSortDto {}

export class ManagerUpdateDto extends idByPostDto {
  @ApiProperty({
    example: {
      manager_name: '小明',
      password: 'wangjing123',
      eamil: '447405759@qq.com',
      phone: '18381532620',
    },
  })
  @Type(() => ManagerInsetDto)
  @ValidateNested({ message: '新数据不符合插入条件' })
  newData: ManagerInsetDto;
}

/**
 * 检查需要唯一索引的字段是否存在在判断是否插入
 */
export class ManagerSaveAtferExistDto {
  @ApiProperty({ example: 'phone' })
  field: string;

  @ApiProperty({ example: '18381532620' })
  value: string;

  @ApiProperty({
    example: {
      manager_name: '小明',
      password: 'wangjing123',
      eamil: '447405759@qq.com',
      phone: '18381532620',
    },
  })
  @Type(() => ManagerInsetDto)
  @ValidateNested({ message: 'obj' })
  manager: ManagerInsetDto;
}
